﻿// Author:					Le Xuan Manh
// Created:					2011-7-18
// Last Modified:			2011-7-18

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;

namespace Hutech.Authentication.Library.Data
{
    public static class DBSite
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return ConfigurationManager.AppSettings["MSSQLConnectionString_CustommerService27032013"];
        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {
            if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString_CustommerService27032013"] != null)
            {
                return ConfigurationManager.AppSettings["MSSQLWriteConnectionString_CustommerService27032013"];
            }

            return ConfigurationManager.AppSettings["MSSQLConnectionString_CustommerService27032013"];
        }


        /// <summary>
        /// Inserts a row in the root_Sites table. Returns rows affected count.
        /// </summary>
        /// <param name="siteId"> siteId </param>
        /// <param name="name"> name </param>
        /// <param name="descriptions"> descriptions </param>
        /// <param name="parentId"> parentId </param>
        /// <returns>int</returns>
        public static int Create(
        Guid siteId,
        string name,
        string descriptions,
        Guid parentId)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_Sites_Insert", 4);
            sph.DefineSqlParameter("@SiteId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, siteId);
            sph.DefineSqlParameter("@Name", SqlDbType.NVarChar, -1, ParameterDirection.Input, name);
            sph.DefineSqlParameter("@Descriptions", SqlDbType.NText, ParameterDirection.Input, descriptions);
            sph.DefineSqlParameter("@ParentId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, parentId);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;
        }


        /// <summary>
        /// Updates a row in the root_Sites table. Returns true if row updated.
        /// </summary>
        /// <param name="siteId"> siteId </param>
        /// <param name="name"> name </param>
        /// <param name="descriptions"> descriptions </param>
        /// <param name="parentId"> parentId </param>
        /// <returns>bool</returns>
        public static bool Update(
        Guid siteId,
        string name,
        string descriptions,
        Guid parentId)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_Sites_Update", 4);
            sph.DefineSqlParameter("@SiteId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, siteId);
            sph.DefineSqlParameter("@Name", SqlDbType.NVarChar, -1, ParameterDirection.Input, name);
            sph.DefineSqlParameter("@Descriptions", SqlDbType.NText, ParameterDirection.Input, descriptions);
            sph.DefineSqlParameter("@ParentId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, parentId);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);
        }

        /// <summary>
        /// Deletes a row from the root_Sites table. Returns true if row deleted.
        /// </summary>
        /// <param name="siteId"> siteId </param>
        /// <returns>bool</returns>
        public static bool Delete(
        Guid siteId)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_Sites_Delete", 1);
            sph.DefineSqlParameter("@SiteId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, siteId);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);
        }

        /// <summary>
        /// Gets an IDataReader with one row from the root_Sites table.
        /// </summary>
        /// <param name="siteId"> siteId </param>
        public static IDataReader GetOne(
        Guid siteId)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_Sites_SelectOne", 1);
            sph.DefineSqlParameter("@SiteId", SqlDbType.UniqueIdentifier, ParameterDirection.Input, siteId);
            return sph.ExecuteReader();
        }

        /// <summary>
        /// Gets a count of rows in the root_Sites table.
        /// </summary>
        public static int GetCount()
        {
            return Convert.ToInt32(SqlHelper.ExecuteScalar(
            GetReadConnectionString(),
            CommandType.StoredProcedure,
            "root_Sites_GetCount",
            null));
        }

        /// <summary>
        /// Gets an IDataReader with all rows in the root_Sites table.
        /// </summary>
        public static IDataReader GetAll()
        {
            return SqlHelper.ExecuteReader(
            GetReadConnectionString(),
            CommandType.StoredProcedure,
            "root_Sites_SelectAll",
            null);
        }

        /// <summary>
        /// Gets a page of data from the root_Sites table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
        int pageNumber,
        int pageSize,
        out int totalPages)
        {
            totalPages = 1;
            int totalRows
            = GetCount();

            if (pageSize > 0)
                totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_Sites_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();
        }
    }
}


